import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/app_bg_view.dart';
import 'package:pornhub_app/components/image_view.dart';
import 'package:pornhub_app/components/text_view.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/extension.dart';

import 'withdraw_page_controller.dart';

class WithdrawPage extends GetView<WithdrawPageController> {
  const WithdrawPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: COLOR.color_161616,
      appBar: AppBar(
        backgroundColor: COLOR.color_161616,
        title: Obx(() => TextView(
              text: "${controller.title.value}提现",
              color: COLOR.white,
              fontSize: 18.w,
            )),
      ),
      body: _bodyView(),
    );
  }

  _bodyView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 15.w),
      child: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  10.verticalSpace,
                  Row(
                    children: [
                      Expanded(
                        flex: 1,
                        child: Obx(() => ImageView(
                              src: controller.payType.value == 1
                                  ? AppImagePath.mine_withdraw_aipay_checked
                                  : AppImagePath.mine_withdraw_aipay_uncheck,
                              width: double.infinity,
                              height: 60.w,
                              fit: BoxFit.fill,
                            ).onOpaqueTap(() => controller.payType.value = 1)),
                      ),
                      15.horizontalSpace,
                      Expanded(
                        flex: 1,
                        child: Obx(() => ImageView(
                              src: controller.payType.value == 3
                                  ? AppImagePath.mine_withdraw_bank_checked
                                  : AppImagePath.mine_withdraw_bank_uncheck,
                              width: double.infinity,
                              height: 60.w,
                              fit: BoxFit.fill,
                            ).onOpaqueTap(() => controller.payType.value = 3)),
                      ),
                    ],
                  ),
                  20.verticalSpace,
                  TextView(
                    text: "姓名",
                    color: COLOR.white,
                    fontSize: 16.w,
                  ),
                  16.verticalSpace,
                  _textFieldView(
                    controller: controller.nameTextEditingController,
                    hintText: "请输入姓名",
                  ),
                  20.verticalSpace,
                  Obx(() => TextView(
                        text: controller.payType.value == 1 ? "支付宝账号" : "银行卡账号",
                        color: COLOR.white,
                        fontSize: 16.w,
                        fontWeight: FontWeight.w500,
                      )),
                  16.verticalSpace,
                  Obx(() => _textFieldView(
                          controller: controller.accountTextEditingController,
                          hintText: controller.payType.value == 1
                              ? "请输入支付宝账号"
                              : "请输入银行卡账号",
                          inputType: const TextInputType.numberWithOptions(),
                          inputFormatters: [
                            controller.payType.value == 1
                                ? FilteringTextInputFormatter.deny(RegExp(''))
                                : FilteringTextInputFormatter.allow(
                                    RegExp('[0-9]')),
                          ])),
                  20.verticalSpace,
                  Row(
                    children: [
                      TextView(
                        text: "提现金额",
                        color: COLOR.white,
                        fontSize: 16.w,
                      ),
                      const Spacer(),
                      TextView(
                        text: "全部提现",
                        color: COLOR.color_FF9000,
                        fontSize: 14.w,
                      ).onOpaqueTap(() {
                        controller.amountTextEditingController.text =
                            controller.title.value == "余额"
                                ? "${controller.userService.assets.bala ?? 0}"
                                : "${controller.userService.assets.gold ?? 0}";
                      }),
                    ],
                  ),
                  16.verticalSpace,
                  _textFieldView(
                      controller: controller.amountTextEditingController,
                      hintText: "0",
                      inputType:
                          const TextInputType.numberWithOptions(decimal: true),
                      fontSize: 30.w,
                      prefix: TextView(
                        text: "¥ ",
                        fontSize: 18.w,
                      ),
                      inputFormatters: [
                        FilteringTextInputFormatter.allow(RegExp('[0-9]'))
                      ]),
                  12.verticalSpace,
                  Row(
                    children: [
                      TextView(
                        text: "余额:",
                        color: COLOR.color_999999,
                        fontSize: 12.w,
                      ),
                      Obx(() => TextView(
                            text:
                                "${controller.title.value == "余额" ? controller.userService.assets.bala ?? 0 : controller.userService.assets.gold ?? 0}",
                            color: COLOR.color_FF9000,
                            fontSize: 12.w,
                          )),
                      const Spacer(),
                      Obx(() => TextView(
                            text:
                                "满${controller.withdrawConfig.value.minQuota ?? 0}可提现，最高额度${controller.withdrawConfig.value.maxQuota ?? 0}",
                            color: COLOR.color_999999,
                            fontSize: 12.w,
                          )),
                    ],
                  ),
                  30.verticalSpace,
                  TextView(
                    text: "温馨提示：\n"
                        "1.请填写正确的支付宝账号及姓名，如资料错误可能导致提现失败.\n"
                        "2.提现到账时间为1-2个工作日，请留意银行卡账单状体.\n"
                        "3.你的个人资料将严格保密，不会用于第三方.",
                    color: COLOR.color_999999,
                    fontSize: 12.w,
                  ),
                ],
              ),
            ),
          ),
          AppBgView(
            width: double.infinity,
            height: 42.w,
            radius: 21.w,
            backgroundColor: COLOR.color_FF9000,
            text: "确认提现",
            textColor: COLOR.black,
            onTap: () => controller.applyWithdrawal(),
          ),
          40.verticalSpace,
        ],
      ),
    );
  }

  _textFieldView({
    TextEditingController? controller,
    String? hintText,
    TextInputType? inputType,
    List<TextInputFormatter>? inputFormatters,
    Widget? prefix,
    double? fontSize,
  }) {
    return TextField(
      controller: controller,
      style: TextStyle(
        color: COLOR.white,
        fontSize: fontSize ?? 14.w,
      ),
      decoration: InputDecoration(
        hintText: hintText,
        hintStyle: TextStyle(
          color: COLOR.white,
          fontSize: fontSize ?? 14.w,
        ),
        isCollapsed: false,
        prefix: prefix,
      ),
      keyboardType: inputType,
      inputFormatters: inputFormatters,
    );
  }
}
